package cn.globalph.housekeeper.data.retrofit;

import android.text.TextUtils;

import androidx.annotation.NonNull;


import java.io.IOException;

import cn.globalph.housekeeper.data.TokenStorage;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class HeaderInterceptor implements Interceptor {
    public static int VERSION_CODE;
    public static String VERSION_NAME;
    public static String HTTP_HEADER;
    public static String PLATFORM;
    public static String PLATFORM_VERSION;
    public static String USER_AGENT;

    @Override
    public Response intercept(@NonNull Chain chain) throws IOException {
        Request.Builder builder = chain.request()
                .newBuilder()
                .addHeader("platform", HTTP_HEADER)
                .addHeader("app_version", String.valueOf(VERSION_CODE))
                .addHeader("app_versionName", String.valueOf(VERSION_NAME));

        if (!TextUtils.isEmpty(USER_AGENT))
            builder.removeHeader("User-Agent")
                    .addHeader("User-Agent", USER_AGENT);

        if (!TextUtils.isEmpty(TokenStorage.Companion.getToken()))
            builder.removeHeader("Authorization")
                    .addHeader("Authorization", "Bearer " + TokenStorage.Companion.getToken());
        Request request = builder.build();
        return chain.proceed(request);
    }
}
